package com.kyexpress.vms.util.baidu;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kyexpress.vms.enitity.DataCenter;
import com.kyexpress.vms.enitity.MyConfig;
import com.kyexpress.vms.util.MyNotifier;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class Fanyi4BaiDu {

    private static final String BAI_DU_URL = "https://fanyi-api.baidu.com/api/trans/vip/translate";
    
    private static   ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws IOException {

        String q = "中国精神";
        // String content = translate(q);
        // System.out.println(content);
        String s1 = getUpCaseString(q);
        System.out.println(s1);
        String digest = getDigest("2015063000000001apple143566028812345678");
        System.out.println(digest);
    }
    
    @NotNull
    public static String getUpCaseString(String translate) throws IOException {
        String content = translate(translate);
        String[] s = content.split(" ");
        StringBuilder stringBuffer = new StringBuilder();
        for (String s1 : s) {
            stringBuffer.append( s1.toUpperCase());
            stringBuffer.append("_");
        }
        stringBuffer.deleteCharAt(stringBuffer.length()-1);
        return stringBuffer.toString();
    }
    
    public static String translate(String q) throws IOException {
        Map<String,String> params = new HashMap<String,String>();
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("from", "zh");
        params.put("to", "en");
        String signStr = MyConfig.getInstance().app_id + q + salt +   MyConfig.getInstance().app_secret;
        String sign = getDigest(signStr);
        params.put("appid",  MyConfig.getInstance().app_id);
        params.put("q", q);
        params.put("salt", salt);
        params.put("sign", sign.toLowerCase());
        // params.put("vocabId","您的用户词表ID");
        /** 处理结果 */
        return requestForHttp(BAI_DU_URL, params);
    }
    
    public static String requestForHttp(String url, Map<String,String> params) throws IOException {

        /** 创建HttpClient */
        CloseableHttpClient httpClient = HttpClients.createDefault();

        /** httpPost */
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
        Iterator<Map.Entry<String,String>> it = params.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String,String> en = it.next();
            String key = en.getKey();
            String value = en.getValue();
            paramsList.add(new BasicNameValuePair(key,value));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(paramsList,"UTF-8"));
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        try{
            Header[] contentType = httpResponse.getHeaders("Content-Type");
            if("audio/mp3".equals(contentType[0].getValue())){
                //如果响应是wav
                HttpEntity httpEntity = httpResponse.getEntity();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                httpResponse.getEntity().writeTo(baos);
                byte[] result = baos.toByteArray();
                EntityUtils.consume(httpEntity);
                if(result != null){//合成成功
                    String file = "合成的音频存储路径"+System.currentTimeMillis() + ".mp3";
                    byte2File(result,file);
                }
            }else{
                /** 响应不是音频流，直接显示结果 */
                HttpEntity httpEntity = httpResponse.getEntity();
                String json = EntityUtils.toString(httpEntity,"UTF-8");
                EntityUtils.consume(httpEntity);
                Map map = objectMapper.readValue(json, Map.class);
                if (map.get("error_msg") != null) {
                   throw  new RuntimeException((String) map.get("error_msg"));
                }
    
               return  ((ArrayList<Map<String,String>>) map.get("trans_result")).get(0).get("dst");
            }
        }finally {
            try{
                if(httpResponse!=null){
                    httpResponse.close();
                }
            }catch(IOException e){
                System.err.println("## release resouce error ##"+e.getMessage());
            }
        }
        return "no found";
    }

    /**
     * 生成加密字段
     */
    public static String getDigest(String string) {
        if (string == null) {
            return null;
        }
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        byte[] btInput = string.getBytes(StandardCharsets.UTF_8);
        try {
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }

    /**
    *
    * @param result 音频字节流
    * @param file 存储路径
    */
    private static void byte2File(byte[] result, String file) {
        File audioFile = new File(file);
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream(audioFile);
            fos.write(result);

        }catch (Exception e){
            System.out.println(e.toString());
        }finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

  public static String truncate(String q) {
        if (q == null) {
            return null;
        }
        int len = q.length();
        String result;
        return len <= 20 ? q : (q.substring(0, 10) + len + q.substring(len - 10, len));
    }
    
}